<?php

namespace app\administration_module\controller;

use \app\common\controller\Base;
use \think\Validate;

class Index extends Base
{
    public function index()
    {
        $this->redirect("administration_module/index/show_list");//页面的跳转
    }

    /*
     * 学生信息查询
     */
    public function show_list()
    {
        $name = input("name") ? input("name") : "";//搜索的名字
//        $term['s.status'] = "1";
        $term['c.name'] = ['like', "%" . $name . "%"];

        $student_list = db("student")
            ->alias('s')
            ->join('consultant c', 'c.student_id = s.id', 'LEFT')
            ->where($term)
            ->where('s.status_id', '<>', 5)
            ->order('s.class_id asc')
            ->field('c.name,s.*')
            ->paginate(6);

        $this->assign([
            'student_list' => $student_list
            , 'name' => $name
        ]);
        return view();
    }

    /*
     * 根据id获取学生详情信息
     */
    public function get_stu_info($now_student)
    {
        $res = db("student")
            ->alias("s")
            ->join("consultant c", "c.id = s.consultant_id")
            ->join("class cs", "s.class_id = cs.id")
            ->join("staff st", "s.staff_id = st.id")
            ->join("student_status ss", "ss.id = s.status_id")
            ->where("s.id=$now_student")
            ->field("s.id,c.name,c.phone,c.school,c.major,cs.name as class_name,st.name as staff_name,s.id_card,s.is_computer,s.evaluation,ss.type")
            ->select();
        return $res;
    }

    /*
     * 展示咨询者记录
     */
    public function study_record($now_consultant="")
    {
        $study_list = db("student_study_record")->where("student_id",$now_consultant)->select();
        return $study_list;
    }

    /*
     * 添加学生信息的页面展示
     */
    public function show_add()
    {
        $now_consultant = input("now_consultant");
        $student_list = db('consultant')->where('id', $now_consultant)->field('id,name')->find();
        $class_list = db('class')->select();
        $this->assign([
            'student_list' => $student_list
            , 'class_list' => $class_list
        ]);
        return view("index/show_add");
    }

    /*
     * 根据ID拿取学生信息的页面展示
     */
    public function show_edit()
    {
        $now_student = input("now_student");
        $now_name = input("now_name");
        $student_list = db('student')->where('id', $now_student)->find();
        $class_list = db('class')->select();
        $status_list = db('student_status')->select();
        $this->assign([
            'student_list' => $student_list
            , 'class_list' => $class_list
            , 'name' => $now_name
            , 'status_list' => $status_list
        ]);
        return view("index/show_edit");
    }

    /*
     * 根据ID修改学生信息或添加学生信息
     */
    public function commit_add_edit($now_consultant = "", $now_student = "")
    {
        $id_card = input("id_card");
        $class_id = trim(input("class_id"));
        $tuition = input("tuition");
        $type = trim(input("type"));
        $pay_date = trim(input("pay_date"));
        $pay_remark = trim(input("pay_remark"));
        $is_computer = input("is_computer");
        $computer_remark = input("computer_remark");
        $evaluation = trim(input("evaluation"));
        $become_stu_date = time();
        $msg = "";

        /*
         * 检查是否有重复的身份证号码
         */
        if (empty($now_student)) {
            $res_id_card = db("student")
                ->alias('s')
                ->join('consultant c', 's.consultant_id = c.id', 'LEFT')
                ->join('class cs', 's.class_id = cs.id', 'LEFT')
                ->where(['s.id_card' => $id_card])
                ->field("c.name as student_name,cs.name as class_name")
                ->select();
            if ($res_id_card) {
                alert("身份证号码已存在!学生名称为：" . $res_id_card[0]["student_name"] . ";所属班级为:" . $res_id_card[0]["class_name"], "error");
            }
        }

        if (empty($id_card) == 1 || id_card($id_card) == 0) {
            $msg = '身份证号码格式错误';
        } else if (empty($tuition) == 1) {
            $msg = '应缴纳学费不能为空';
        } else if (empty($pay_date) == 1) {
            $msg = '缴纳日期不能为空';
        } else if (is_num($tuition) == 0) {
            $msg = '应缴纳学费只能是数字类型';
        } else if (is_date($pay_date) == 0 || Validate::is($pay_date, 'date') == 0) {
            $msg = '出生日期格式错误';
        }
        if ($msg != "") {
            alert($msg, "error");
        }
        $info = [
            'consultant_id' => $now_consultant
            , 'id_card' => $id_card
            , 'staff_id' => session("user_id")
            , 'class_id' => $class_id
            , 'tuition' => $tuition
            , 'type' => $type
            , 'pay_date' => $pay_date
            , 'pay_remark' => $pay_remark
            , 'is_computer' => $is_computer
            , 'computer_remark' => $computer_remark
            , 'evaluation' => $evaluation
        ];
        if (empty($now_student)) {
            $info["become_stu_date"] = $become_stu_date;
            $student = model('administration');
            $student->data($info);
            $student->save();
            $student_id = $student->id;
            $res = db('consultant')->where('id', $now_consultant)->update(['is_student' => 1, 'student_id' => $student_id]);
            if ($res) {
                alert("添加成功", "success", "/administration_module/index/show_list");
            } else {
                alert("添加失败", "error");
            }
        } else {
            $status_id = trim(input("status_id"));
            $status_remark = trim(input("status_remark"));
            $is_restudy = trim(input("is_restudy"));
            $leave_s = trim(input("leave_s"));
            $leave_e = trim(input("leave_e"));
            $exit_reason = trim(input("exit_reason"));
            $info["status_id"] = $status_id;
            $info["status_remark"] = $status_remark;
            $info["is_restudy"] = $is_restudy;
            $info["leave_s"] = $leave_s;
            $info["leave_e"] = $leave_e;
            $info["exit_reason"] = $exit_reason;
            $res = db('student')->where('id', $now_student)->update($info);
            if ($res) {
                alert("更新成功", "success", "/administration_module/index/show_list");
            } else {
                alert("更新失败", "error");
            }
        }
    }

    /*
     * 根据ID删除学生信息
     * update()
     */
    public function delete()
    {
        $id = input("param.id");
        $res = db('student')->where('id', $id)
            ->update(['status_id' => "0"]);
        if ($res) {
            $status = 1;
            $msg = "删除成功";
        } else {
            $status = 0;
            $msg = "删除失败";
        }
        $return = [
            "status" => $status
            , "res" => $id
            , "msg" => $msg
        ];
        return $return;
    }

    /*
     * 展示恢复学生
     */
    public function show_restore()
    {
        $name = input("name") ? input("name") : "";//搜索的名字
        $term['s.status_id'] = "5";
        $term['c.name'] = ['like', "%" . $name . "%"];

        $student_list = db("student")
            ->alias('s')
            ->join('consultant c', 'c.student_id = s.id', 'LEFT')
            ->where($term)
            ->order('s.class_id asc')
            ->field('c.name,s.*')
            ->paginate(6);

        $this->assign([
            'student_list' => $student_list
            , 'name' => $name
        ]);
        return view();
    }

    /*
	 * 根据ID恢复咨询者信息
	 */
    public function confirm_restore()
    {
        $id = input("param.id");
        $res = db('student')->where('id', $id)
            ->update(['status_id' => "1"]);
        if ($res) {
            $status = 1;
            $msg = "恢复成功";
        } else {
            $status = 0;
            $msg = "恢复失败";
        }
        $return = [
            "status" => $status
            , "res" => $id
            , "msg" => $msg
        ];
        return $return;
    }
}
